class Solution 
{
private:
    int Find(vector<int>& parent, int index) 
    {
        if(parent[index] != index)
        {
            parent[index] = Find(parent, parent[index]);
        }
        return parent[index];
    }

    void Union(vector<int>& parent, int index1, int index2)
    {
        parent[Find(parent, index1)] = Find(parent, index2);
    }

public:
    vector<int> findRedundantConnection(vector<vector<int>>& edges) 
    {
        int numOfNodes = edges.size() + 1;
        vector<int> parent(numOfNodes, 0);
        for (size_t i = 1; i < numOfNodes; i++)
        {
            parent[i] = i;
        }

        for (size_t i = 0; i < edges.size(); i++)
        {
            int node1 = edges[i][0], node2 = edges[i][1];
            if (Find(parent, node1) == Find(parent, node2))
            {
                return edges[i];
            }
            else
            {
                Union(parent, node1, node2);
            }
        }
        return vector<int>{};

    }
};